/*
 * @Description:  笔记
 * @Author: Myruun
 * @Date: 2021-04-22 09:36:31
 * @LastEditors: Myruun
 * @LastEditTime: 2021-07-29 10:53:38
 * @FilePath: \serve\controller\note.js
 */

const mysqlQuery = require("../unit/mysql.js");

// 获取笔记列表
exports.Index = async (ctx, next) => {
  let body = ctx.request.body;
  const id = body.id || "";
  const type = body.type || "";

  let sql = "";
  if (id) {
    sql =
      "SELECT * FROM note  WHERE type = " + type + " and id ='" +
      id +
      "' order by id desc";
  } else {
    sql = "SELECT * FROM note  WHERE type = " + type + " order by id desc";
  }
  let obj = await mysqlQuery(sql);
  ctx.response.body = obj;
};

// 添加笔记
exports.add = async (ctx, next) => {
  let body = ctx.request.body;
  const content = body.content || "";
  const title = body.title || "";
  const describe = body.describe || "";
  const type = body.type || "";
  const priority = body.priority || 0;
  const time = Math.round(new Date() / 1000);
  let sql = `INSERT INTO note (id,title,content,creation_time,updata_time,type,state,describes,priority) VALUES (0,"${title}",'${content}',"${time}","${time}","${type}",1,"${describe}","${priority}")`;
  let obj = await mysqlQuery(sql);
  ctx.response.body = obj;
};
